// ServiceConfig.cs
//

namespace ServiceAdmin
{
	using System;
	using System.Collections;
	using System.Collections.Specialized;
	using System.ComponentModel;
	using System.Diagnostics;
	using System.Drawing;
	using System.Globalization;
	using System.IO;
	using System.Resources;
	using System.Windows.Forms;


	/// <summary>
	/// Summary description for ServiceConfig.
	/// </summary>
	public class ServiceConfig : Form
	{
		private	Point			    _ctrlTopLeft		= new Point(0, 0);
		private Panel			    _panelToAdd			= null;
		private int				    _controlMargin		= 0;
		private string			    _resXPath			= null;
		private string			    _languageField		= null;

		private string			    _restartMessage		= null;
		private string			    _saveMessage		= null;

		private Hashtable		    _registredValues	= new Hashtable();
		private NameValueCollection _stringValues		= new NameValueCollection();
		public event EventHandler OnLanguageChange;


		public string RestartMessage
		{
			get { return _restartMessage; }
			set { _restartMessage = value; }
		}


		public string SaveMessage
		{
			get { return _saveMessage; }
			set { _saveMessage = value; }
		}


		public ServiceConfig(Panel toWrite, Point topLeftMargin, int marginBetween, string resXPath, string languageFieldName)
		{
			_ctrlTopLeft	= topLeftMargin;
			_panelToAdd		= toWrite;
			_controlMargin	= Math.Abs(marginBetween);

			_resXPath		= resXPath;
			_languageField	= languageFieldName;
			InitValues();
		}


		public void SaveChanges(object sender, EventArgs e)
		{
			IResourceWriter	writer	= new ResXResourceWriter(_resXPath);
			bool			changed	= false;

			foreach (DictionaryEntry myEntry in _registredValues)
			{
				if (WriteConfigEntry(writer, (string)myEntry.Key, (Control)myEntry.Value))
				{
					changed = true;
				}
			}
			writer.Close();

			if (changed && _restartMessage != null && SaveMessage != null)
			{
				MessageBox.Show(RestartMessage, SaveMessage, MessageBoxButtons.OK, MessageBoxIcon.Information);
			}
		}


		private bool WriteConfigEntry(IResourceWriter writer, string key, Control field)
		{
			if (InitNewLanguage(key, field))
			{
				writer.AddResource(key, field.Text);

				if (_stringValues[key] != field.Text)
				{
					_stringValues[key] = field.Text;
					return true;
				}
			}
			return false;
		}


		private bool InitNewLanguage(string keyName, Control newItem)
		{
			if (keyName == _languageField)
			{
				string currentCulture = Application.CurrentCulture.Name;

				try
				{
					Application.CurrentCulture = new CultureInfo(newItem.Text);

					if (OnLanguageChange != null)
					{
						OnLanguageChange(this, new EventArgs());
					}
				}
				catch
				{
					newItem.Text = currentCulture;
					Application.CurrentCulture = new CultureInfo(currentCulture);
					return false;
				}
			}
			return true;
		}


		private void InitValues()
		{
			IResourceReader	reader	= new ResXResourceReader(_resXPath);
			int				counter	= 0;

			foreach(DictionaryEntry myEntry in reader)
			{
				Label description		= new Label();
				description.Location	= new Point(_ctrlTopLeft.X, _ctrlTopLeft.Y + counter + 2);
				description.Width		= 100;
				description.Text		= myEntry.Key.ToString();
				_panelToAdd.Controls.Add(description);

				TextBox optionValue		= new TextBox();
				optionValue.Location	= new Point(_ctrlTopLeft.X + description.Width + 10, _ctrlTopLeft.Y + counter);
				optionValue.Width		= 100;
				optionValue.Text		= myEntry.Value.ToString();
				optionValue.Name		= myEntry.Key.ToString();
				optionValue.Leave	   += new EventHandler(SaveChanges);
				_panelToAdd.Controls.Add(optionValue);

				_registredValues.Add(myEntry.Key, optionValue);
				_stringValues.Add(optionValue.Name, optionValue.Text);
				counter += _controlMargin;
			}
			reader.Close();
		}
	}
}